$(function () {

    var zdj = {
        grid:$("#grid_zdj"),
        gridOptions:{
            url:$.webPath() + "/sys/zdj/zdjList",
            toolbar:'#toolbar_zdj',
            fitColumns:true,
            fit:true,
            rownumbers:true,
            pageNumber:1,
            pagination:true,
            singleSelect:true,
            columns:[
                [
                    {field:'id', title:'Id', width:100,hidden:true},
                    {field:'pm_name', title:'品名', width:100},
                    {field:'zdj', title:"指导价", editor:{type:'text'}, width:100},
                    {field:'drrq', title:'导入日期', width:100},
                    {field:'dateCreated', title:'创建日期',width:100},
                    {field:'lastUpdated', title:'修改日期',width:100},
                    {field:'cz', title:'操作', formatter:function (value, row, index) {
                        if (value && value == 'save')
                            return '<a index="' + index + '" name="save" href="javascript:void(0)" onclick="save_zdj(' + index + ')"></a>'
                        return '<a index="' + index + '" name="edit" href="javascript:void(0)" onclick="edit_zdj(' + index + ')"></a>'
                    }, width:30, align:'center'}
                ]
            ],
            onLoadSuccess:function () {
                $("a[name='edit']").linkbutton({
                    plain:true,
                    iconCls:'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onBeforeEdit:function (rowIndex, rowData) {
                zdj.grid.edatagrid("updateRow", {
                    index:rowIndex,
                    row:$.extend({}, rowData, {cz:'save'})

                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onAfterEdit:function (index, row) {
                var url = $.webPath() + '/sys/zdj/save_zdj';
                $.post(url, row, function (data) {
                    if (data.statusCode == 200) {
                        zdj.grid.datagrid('updateRow', {
                            index:index,
                            row:$.extend({}, row, {cz:'edit','id':data.id,'zdj':data.zdj})
                        });
                        $("a[name='edit']").linkbutton({
                            plain:true,
                            iconCls:'icon-edit'
                        });
                    } else {
                        $.messager.alert('info', data.message);
                        zdj.grid.edatagrid("beginEdit", index);
                    }
                }, 'json');
            }
        }
    }
    zdj.grid.edatagrid(zdj.gridOptions);

    //查询按钮
    $("#btn_zdj_search").click(function () {
        zdj_search();
    });



    //批量入账
    $("#btn_zdj_pldr").click(function(){
        var win = $.window({
            title:"批量导入",
            width:450,
            height:240,
            cache:false,
            url:$.webPath() + "/sys/zdj/zdj_upload",
            modal:true ,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $('#zdj_uploadSubject').form('submit', {
                            onSubmit:function(){
                                $(win).mask();
                            } ,
                            success:function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#grid_zdj').datagrid('reload');
                                    $.messager.show({
                                        title:'提示',
                                        msg:json.message,
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });

});

//查询仓库
function zdj_search() {
    $('#grid_zdj').datagrid('load',
        {
            pmName:$('#zdj_pm').val(),
            zdjStartDate:$("[name='zdjStartDate']").val(),
            zdjEndDate:$("[name='zdjEndDate']").val()
        }
    );
}

function save_zdj(index) {
    $("#grid_zdj").edatagrid("endEdit", index);
}
function edit_zdj(index) {
    $("#grid_zdj").edatagrid("beginEdit", index);
}